package com.tg.app.activity.tools;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.CompoundButton;
import android.widget.Spinner;
import android.widget.Switch;

import com.base.BaseActivity;
import com.tg.app.R;
import com.tg.appcommon.android.TGResources;
import com.tg.login.activity.LoginActivity;

public class PageDevToolsActivity extends BaseActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);//remove title bar  即隐藏标题栏
        setContentView(R.layout.activity_page_dev_tools);
        hideActionBar();

        initView();
    }

    @Override
    public boolean shouldSetStatusBarColor() {
        return true;
    }

    @Override
    protected void initView() {
        findViewById(R.id.back_toolbar).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        initGlobalUiSwitch();
        initLoginLayoutSwitch();
        initLoginLayoutButtonStyleSwitch();
        initLoginLayoutWechatButtonStyleSwitch();
        initLoginLayoutEditTextButtonStyleSwitch();

        initHomeLayoutSwitch();
        initDeviceListPageLayoutSwitch();
        initMinePageLayoutSwitch();

        findViewById(R.id.restart_app_tv).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
                restartApplication();
            }
        });
    }

    private void initHomeLayoutSwitch() {
        final Spinner spinnerLoginLayout = findViewById(R.id.home_page_style_select);
        final int selectedLoginLayoutIndex = TGResources.readHomePageStyle(this);
        spinnerLoginLayout.setSelection(selectedLoginLayoutIndex);
        spinnerLoginLayout.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
                TGResources.writeHomePageStyle(PageDevToolsActivity.this, pos);
                initMinePageLayoutSwitch();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                // Another interface callback
            }
        });
    }

    private void initDeviceListPageLayoutSwitch() {
        final Spinner spinnerLoginLayout = findViewById(R.id.device_list_page_style_select);
        final int selectedLoginLayoutIndex = TGResources.readDeviceListPageStyle(this);
        spinnerLoginLayout.setSelection(selectedLoginLayoutIndex);
        spinnerLoginLayout.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
                TGResources.writeDeviceListPageStyle(PageDevToolsActivity.this, pos);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                // Another interface callback
            }
        });
    }

    private void initMinePageLayoutSwitch() {
        final Spinner spinnerLoginLayout = findViewById(R.id.mine_page_style_select);
        final int selectedLoginLayoutIndex = TGResources.readMinePageStyle(this);

        final int selectedHomeLayoutIndex = TGResources.readHomePageStyle(this);
        String[] testArray = getResources().getStringArray(R.array.mine_page_style_list_standard);
        if (selectedHomeLayoutIndex == 0) { // 首页-标准
            testArray = getResources().getStringArray(R.array.mine_page_style_list_standard);
        } else { // 双模式
            testArray = getResources().getStringArray(R.array.mine_page_style_list_double_mode);
        }

        ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(
                this, android.R.layout.simple_spinner_dropdown_item, testArray );
        spinnerLoginLayout.setAdapter(spinnerArrayAdapter);

        spinnerLoginLayout.setSelection(selectedLoginLayoutIndex);
        spinnerLoginLayout.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
                TGResources.writeMinePageStyle(PageDevToolsActivity.this, pos);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                // Another interface callback
            }
        });
    }

    private void initGlobalUiSwitch() {
        Switch pageUiLayoutSwitch = findViewById(R.id.page_ui_layout_switch);
        boolean uiSwitchEnabled = TGResources.readUiCustomizeSwitch(this);
        pageUiLayoutSwitch.setChecked(uiSwitchEnabled);
        pageUiLayoutSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                TGResources.setUiCustomizeSwitch(buttonView.getContext(), isChecked);
            }
        });
    }

    private void initLoginLayoutSwitch() {
        final Spinner spinnerLoginLayout = findViewById(R.id.login_layout_select);
        final String selectedLoginLayoutStr = TGResources.readRedirectConfig(this, LoginActivity.class.getSimpleName());
        int selectedLoginLayoutIndex = 0;
        String[] array = getResources().getStringArray(R.array.login_layout_list);
        for (int i = 0; i < array.length; i++) {
            if (array[i].equals(selectedLoginLayoutStr)) {
                selectedLoginLayoutIndex = i;
                break;
            }
        }
        final int fselectedLoginLayoutStr = selectedLoginLayoutIndex;
        spinnerLoginLayout.setSelection(selectedLoginLayoutIndex);
        spinnerLoginLayout.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
                TGResources.writeRedirectConfig(PageDevToolsActivity.this, LoginActivity.class.getSimpleName(), array[pos]);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                // Another interface callback
            }
        });
    }

    private void initLoginLayoutButtonStyleSwitch() {
        final Spinner spinnerLoginLayout = findViewById(R.id.page_element_style_button_select);
        final int selectedLoginLayoutIndex = TGResources.readLoginButtonStyle(this);
        spinnerLoginLayout.setSelection(selectedLoginLayoutIndex);
        spinnerLoginLayout.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
                TGResources.writeLoginButtonStyle(PageDevToolsActivity.this, pos);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                // Another interface callback
            }
        });
    }

    private void initLoginLayoutWechatButtonStyleSwitch() {
        final Spinner spinnerLoginLayout = findViewById(R.id.page_element_style_wechat_button_select);
        final int selectedLoginLayoutIndex = TGResources.readLoginWechatButtonStyle(this);
        spinnerLoginLayout.setSelection(selectedLoginLayoutIndex);
        spinnerLoginLayout.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
                TGResources.writeLoginWechatButtonStyle(PageDevToolsActivity.this, pos);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                // Another interface callback
            }
        });
    }

    private void initLoginLayoutEditTextButtonStyleSwitch() {
        final Spinner spinnerLoginLayout = findViewById(R.id.page_element_style_edittext_select);
        final int selectedLoginLayoutIndex = TGResources.readLoginEditTextStyle(this);
        spinnerLoginLayout.setSelection(selectedLoginLayoutIndex);
        spinnerLoginLayout.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
                TGResources.writeLoginEditTextButtonStyle(PageDevToolsActivity.this, pos);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                // Another interface callback
            }
        });
    }

    /**
     * 重启应用
     */
    private void restartApplication() {
        Intent intent = getPackageManager().getLaunchIntentForPackage(getApplication().getPackageName());
        PendingIntent pendingIntent;
        if (android.os.Build.VERSION.SDK_INT >= 23) {
            pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_IMMUTABLE);
        }else {
            pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
        }
        AlarmManager mgr = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
        mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 500, pendingIntent); // 500ms后重启应用
        System.exit(0);
    }
}